﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/
using System;
using System.ComponentModel;
using Newtonsoft.Json;

namespace OI.DataModel
{
    public class OIApp : IEditableObject, IDataErrorInfo 
    {
        public string Title
        {
            get 
            {
                return _Title; 
            }
            set
            {
                _Title = value;
            }
        }
        [JsonIgnore]
        private string _Title = "[Unnamed]";

        public int ID { get; set; }
        public int JurisdictionFK { get; set; }
        public string Description { get; set; }
        public string ThumbnailURI { get; set; }
        public string UriOverride { get; set; }

        public Guid ShareID
        {
            get { return _ShareID; }
            set { _ShareID = value; }
        }
        [JsonIgnore]
        private Guid _ShareID = Guid.Empty;


        // support undo in editors
        [JsonIgnore]
        private string Checkpoint { get; set; }

        [JsonIgnore]
        private bool InEdit { get; set; }

        [JsonIgnore]
        public Action PostCancelAction { get; set; }

        [JsonIgnore]
        public Func<OIApp, string, string> PostValidationFunction { get; set; }

        public void BeginEdit()
        {
            Checkpoint = MessageSerializer.Serialize(this);
            InEdit = true;
        }

        public void CancelEdit()
        {
            if (!string.IsNullOrEmpty(Checkpoint))
            {
                var checkpoint = MessageSerializer.Deserialize(Checkpoint) as OIApp;
                if (checkpoint != null)
                {
                    ID = checkpoint.ID;
                    ShareID = checkpoint.ShareID;
                    Title = checkpoint.Title;
                    Description = checkpoint.Description;
                    ThumbnailURI = checkpoint.ThumbnailURI;
                }
            }

            if (PostCancelAction != null)
            {
                PostCancelAction();
            }

            InEdit = false;
        }

        public void EndEdit()
        {
            InEdit = false;
        }

        public string Error
        {
            get { return null; }
        }

        // data validation
        public string this[string columnName]
        {
            get
            {
                // only validate when we are editing
                if (!InEdit)
                {
                    return null;
                }

                switch (columnName)
                {
                    case "Title":
                        if (string.IsNullOrEmpty(Title))
                        {
                            return "must be provided for an app!";
                        }
                        break;
                    case "ShareID":
                        if (ShareID == Guid.Empty)
                        {
                            return "must be provided for an app!";
                        }
                        break;
                }

                if (PostValidationFunction != null)
                {
                    return PostValidationFunction(this, columnName);
                }
                return null;
            }
        }
    }

}